package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
)

type HelloController struct {
}

func NewHelloController() *HelloController {
	return &HelloController{}
}

type Test struct {
	Id   string `json:"id"`
	Name string `json:"name"`
}

func (controller *HelloController) GetTest(c *gin.Context) {
	tests := Test{
		Id:   "1",
		Name: "test1",
	}
	c.JSON(http.StatusOK, tests)
}
func (controller *HelloController) PostTest(c *gin.Context) {
	//转换body为map
	var test map[string]string
	if err := c.ShouldBindJSON(&test); err != nil {
		fmt.Println("============err========")
	}
	num := rand.Intn(10)
	if num > 5 {
		myErr := &gin.Error{
			Err:  fmt.Errorf("测试错误信息"),
			Type: gin.ErrorTypePublic,
			Meta: "busError",
		}
		c.Errors = append(c.Errors, myErr)
		return
	}
	c.JSON(http.StatusOK, test)
}
func (controller *HelloController) DeleteTest(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "DeleteTest",
		"data": nil,
	})
}
func (controller *HelloController) PutTest(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "PutTest",
		"data": nil,
	})
}

func (controller *HelloController) OptionsTest(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "OptionsTest",
		"data": nil,
	})
}
